import random
from secrets import choice


class Guess:
    def __init__(self):
        self.max_number = int(input("数字区间将从1开始，请输入截止的数字："))  # 设定最大数字
        self.max_count = int(input("请设定可以猜几次："))  # 设定竞猜次数
        self.start()

    # step_2: 汇总规则
    def info(self, max_number, max_count):
        print("\n--------------------------------------")
        print(
            """
        游戏规则：
        1、程序将在【{}-{}】间随机选取一个数字；
        2、请每次猜一个数字；
        3、最多可以猜【{}】次；
        4、指定次数内猜中即胜利，否则判定失败。
        """.format(
                1, max_number, max_count
            )
        )
        print("--------------------------------------\n")

    # step_3: 执行运算
    def guess_number(self):
        target_number = random.randint(1, self.max_number)  # 系统随机选定一个数字
        guess_history = []  # 记录猜数过程
        for i in range(self.max_count):
            # 判断输入的字符是否合规————指定范围内的数字
            while True:
                num_list = [x for x in range(1, self.max_number + 1)]
                guess_num = input("已经猜过的数字有：{}\n请猜一个数字：".format(guess_history))
                if guess_num == "":
                    print("\n--------------------------------------")
                    print("输入错误！你未输入任何字符！请输入1-{}的数字！".format(self.max_number))
                    print("--------------------------------------\n")
                    continue
                else:
                    if guess_num.isdigit():
                        if int(guess_num) in num_list:
                            break
                        else:
                            print("\n--------------------------------------")
                            print(
                                "输入错误！你输入的数字不在指定范围内！请输入1-{}的数字！".format(self.max_number)
                            )
                            print("--------------------------------------\n")
                            continue
                    else:
                        print("\n--------------------------------------")
                        print("输入错误！你输入的不是数字！请输入1-{}的数字！".format(self.max_number))
                        print("--------------------------------------\n")

            guess_num = int(guess_num)
            guess_history.append(guess_num)
            if guess_num == target_number:
                print("\n--------------------------------------")
                print("恭喜！猜对了")
                print("猜了【{}】次，竞猜过程是{}\n".format(len(guess_history), guess_history))
                break
            elif guess_num > target_number:
                print("猜大了\n")
            elif guess_num < target_number:
                print("猜小了\n")
            else:
                print("逻辑出错")
        else:
            print("-------------------------")
            print("很遗憾{}次都猜错了,你猜的数字是：{}".format(self.max_count, guess_history))
            print("正确的数字是：{}".format(target_number))
            print("-------------------------")

    def start(self):
        self.info(self.max_number, self.max_count)
        self.guess_number()


while True:
    choice = input("\n开始请输入数字【1】，退出请输入数字【2】：")
    print("-------------------------\n")
    if choice == "1":
        Guess()
    elif choice == "2":
        break
    else:
        print("输入错误，请重新输入！")
